package com.nondo.dean.platform.service;

import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.platform.domain.Region;
import com.nondo.dean.platform.domain.RegionExample;
import com.nondo.dean.platform.domain.User;
import com.nondo.dean.platform.mapper.RegionMapper;
import com.nondo.dean.platform.model.UserBean;
import com.nondo.dean.platform.model.result.AreaFindUserResult;
import com.nondo.dean.platform.model.result.ChildrenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xiaofan
 * @Description: 客户区域管理业务类
 * @throws
 * @date 2018-04-23 14:21
 * @Version 0.1
 */
@Component
public class RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private UserService userService;


    /**
     * 根据区域编码得到区域名称
     * @param code
     * @return
     */
    public String getRegionName(String code){
        String regionName="";//区域名称
        RegionExample regionExample= new RegionExample();
        regionExample.createCriteria().andCodeEqualTo(code);
        List<Region> regionList= regionMapper.selectByExample(regionExample);
        for(Region regions : regionList){
            regionName=regions.getName();
        }
        return regionName;
    }
    
    /**
     * 按照主键ID查询客户区域
     * @param id
     * @return
     */
    public Region selectRegion(Integer id) {
        return regionMapper.selectByPrimaryKey(id);
    }

    
    /**
     * 客户区域列表
     * @return 所有客户区域数据
     */
    public List<Region> getRegionList() {
        RegionExample example = new RegionExample();
        example.setOrderByClause("create_date DESC");
        RegionExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(Region.DEL_FLAG_NORMAL);
        return regionMapper.selectByExample(example);
    }

    /**
     * 查询区域和区域下面的经销商
     * @return
     */
    public List<AreaFindUserResult> areaFindUser(Integer[] ids){
        List<Integer> regionIds = Arrays.asList(ids);
        List<ChildrenUser> childrenUserList =new ArrayList<>();
        List<AreaFindUserResult> newList = new ArrayList<>();
        List<Region> regionList = searchRegionList(regionIds);
        for(Region beans : regionList){
            AreaFindUserResult areaFindUserResult = new AreaFindUserResult();
            areaFindUserResult.setKey(beans.getId());
            areaFindUserResult.setValue(beans.getId());
            areaFindUserResult.setLabel(beans.getName());
            User user = new User();
            user.setRegionId(beans.getId());
            List<UserBean> userBeanList = userService.getUserList(user);
            for(UserBean userBeans : userBeanList){
                ChildrenUser childrenUser = new ChildrenUser();
                childrenUser.setKey(userBeans.getId());
                childrenUser.setValue(userBeans.getId());
                childrenUser.setLabel(userBeans.getUserName());
                childrenUserList.add(childrenUser);
            }
            areaFindUserResult.setChildren(childrenUserList);
            newList.add(areaFindUserResult);
        }

        return newList;
    }

    /**
     * 客户区域列表根据多个id查询
     * @return 所有客户区域数据
     */
    public List<Region> searchRegionList(List<Integer> ids) {
        RegionExample example = new RegionExample();
        example.setOrderByClause("create_date DESC");
        RegionExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids).andDelFlagEqualTo(Region.DEL_FLAG_NORMAL);
        return regionMapper.selectByExample(example);
    }
}
